#线性编码版本1：执行一条测试用例
import pymysql,requests
conn=pymysql.connect(host='192.168.196.128', db='wa_test', user='root', passwd='123456')
cursor=conn.cursor()
sqls=["delete from info where id=4","delete from users where id =4",
      "insert into users(id,username,password)values(4,'test01',md5('123456'))",
      "insert into info(id,name)values(4,'测试一')",
      "delete from info where id=(select id from users where username='test06');",
      "delete from users where username='test06'"]
for sql in sqls:
      cursor.execute(sql)
conn.commit()
conn.close()
url='http://192.168.196.128/apitest/login/'
case=['login_01','测试登录成功',{'username':'test01','password':'123456'},{'Status':1000,'Result':'Usercheck ok','Message':'登录验证成功'}]#测试用例
case_id,case_name,args,expect=case  #用例解包到变量
res=requests.post(url,args)
print(res.json())
#比对成功登录的响应结果
case_info=case_id+':'+case_name
actual=res.json()
if expect==actual:
      print(f'{case_info}==响应结果比对通过')
else:
      print(f'{case_info}==响应结果比对失败==预期：{expect}==实际：{actual}')
#执行成功注册的测试用例：
url='http://192.168.196.128/apitest/signup/'
case=['signup_01','测试成功注册',{'username':'test06','password':'123456','confirm':'123456','name':'测试六'},{'Status':1000,'Result':'Success','Message':'注册成功'},"select count(*) from users,info where users.id=info.id and username='test06' and password=md5('123456') and name='测试六'",1]
case_id,case_name,args,expect,check_sql,expect_rows=case  #用例解包
res=requests.post(url,json=args)
#比对成功注册的响应结果：
actual=res.json()
case_info=case_id+':'+case_name
if expect==actual:
      print(f'{case_info}==响应结果比对通过')
else:
      print(f'{case_info}==响应结果比对失败==预期：{expect}==实际：{actual}')
#成功注册的落库检查：
conn=pymysql.connect(host='192.168.196.128', db='wa_test', user='root', passwd='123456')
cursor=conn.cursor()
cursor.execute(check_sql)
row=cursor.fetchone()
actual_rows=row[0]
if expect_rows==actual_rows:
      print(f'{case_info}==验库成功')
else:
      print(f'{case_info}==验库失败==要检查的数据：{args}=预期行数：{expect_rows}==实际：{actual_rows}')
conn.close()
